package com.scuec.trainingmanagementsys.service.impl;

import com.scuec.trainingmanagementsys.entity.Project;
import com.scuec.trainingmanagementsys.entity.WeeklyReport;
import com.scuec.trainingmanagementsys.mapper.WeeklyReportMapper;
import com.scuec.trainingmanagementsys.service.ProjectService;
import com.scuec.trainingmanagementsys.service.TeamMemberService;
import com.scuec.trainingmanagementsys.service.WeeklyReportService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class WeeklyReportServiceImpl implements WeeklyReportService {
    @Autowired
    private WeeklyReportMapper weeklyReportMapper;

    @Autowired
    private TeamMemberService teamMemberService;

    @Autowired
    private ProjectService projectService;

    @Override
    public Map<String, Object> addWeeklyReport(WeeklyReport weeklyReport, Integer studentID) {
        Map<String, Object> result = new HashMap<>();

        // 检查teamID是否为null
        if (weeklyReport.getTeamID() == null) {
            result.put("success", false);
            result.put("message", "团队ID不能为空");
            return result;
        }

        // 1. 检查用户是否为团队负责人
        String memberRole = teamMemberService.getMemberRole(weeklyReport.getTeamID(), studentID);
        if (!"leader".equalsIgnoreCase(memberRole)) {
            result.put("success", false);
            result.put("message", "只有团队负责人可以发布周报");
            return result;
        }

        // 2. 设置发布者ID
        weeklyReport.setStudentID(studentID);

        // 3. 执行添加操作
        int rows = weeklyReportMapper.addWeeklyReport(weeklyReport);
        if (rows > 0) {
            result.put("success", true);
            result.put("message", "周报添加成功");
            result.put("data", weeklyReport.getWeeklyReportID());
        } else {
            result.put("success", false);
            result.put("message", "周报添加失败");
        }

        return result;
    }

    @Override
    public Map<String, Object> deleteWeeklyReport(Integer weeklyReportID, Integer studentID) {
        Map<String, Object> result = new HashMap<>();

        // 1. 检查周报是否存在
        WeeklyReport report = weeklyReportMapper.getWeeklyReportById(weeklyReportID);
        if (report == null) {
            result.put("success", false);
            result.put("message", "周报不存在");
            return result;
        }

        // 2. 检查用户是否为发布者或团队负责人
        int isOwner = weeklyReportMapper.isReportOwner(weeklyReportID, studentID);
        String memberRole = teamMemberService.getMemberRole(report.getTeamID(), studentID);
        boolean isLeader = "leader".equalsIgnoreCase(memberRole);

        if (isOwner == 0 && !isLeader) {
            result.put("success", false);
            result.put("message", "只有周报发布者或团队负责人可以删除");
            return result;
        }

        // 3. 执行删除操作
        int rows = weeklyReportMapper.deleteWeeklyReport(weeklyReportID);
        if (rows > 0) {
            result.put("success", true);
            result.put("message", "周报删除成功");
        } else {
            result.put("success", false);
            result.put("message", "周报删除失败");
        }

        return result;
    }

    @Override
    public Map<String, Object> updateWeeklyReport(WeeklyReport weeklyReport) {
        Map<String, Object> result = new HashMap<>();
        int rows = weeklyReportMapper.updateWeeklyReport(weeklyReport);
        if (rows > 0) {
            result.put("success", true);
            result.put("message", "周报更新成功");
        } else {
            result.put("success", false);
            result.put("message", "周报更新失败");
        }
        return result;
    }

    @Override
    public WeeklyReport getWeeklyReportById(Integer weeklyReportID) {
        return weeklyReportMapper.getWeeklyReportById(weeklyReportID);
    }

    @Override
    public List<WeeklyReport> getWeeklyReportsByTeamId(Integer teamID) {
        return weeklyReportMapper.getWeeklyReportsByTeamId(teamID);
    }

    @Override
    public List<WeeklyReport> getWeeklyReportsByStudentId(Integer studentID) {
        return weeklyReportMapper.getWeeklyReportsByStudentId(studentID);
    }

    @Override
    public List<WeeklyReport> getWeeklyReportsByStudentIdWithRelations(Integer studentID) {
        return weeklyReportMapper.getWeeklyReportsByStudentIdWithRelations(studentID);
    }

    @Override
    public boolean isTeamLeader(Integer teamID, Integer studentID) {
        String memberRole = teamMemberService.getMemberRole(teamID, studentID);
        return "leader".equalsIgnoreCase(memberRole);
    }

    @Override
    public List<WeeklyReport> getWeeklyReportsWithProjectName() {
        List<WeeklyReport> reports = weeklyReportMapper.getAllWeeklyReports();
        for (WeeklyReport report : reports) {
            if (report.getProjectID() != null) {
                Project project = projectService.getProject(report.getProjectID());
                if (project != null) {
                    report.setProjectName(project.getProjectName());
                }
            }
        }
        return reports;
    }

    @Override
    public List<WeeklyReport> getWeeklyReportsByTeamIdWithRelations(Integer teamID) {
        return weeklyReportMapper.getWeeklyReportsByTeamIdWithRelations(teamID);
    }

    @Override
    public List<WeeklyReport> getAllWeeklyReports() {
        return weeklyReportMapper.getAllWeeklyReports();
    }

    @Override
    public List<WeeklyReport> getWeeklyReportsByProjectId(Integer projectId) {
        return weeklyReportMapper.getWeeklyReportsByProjectId(projectId);
    }

    @Override
    public List<Map<String, Object>> getWeeklyCountLast7Days() {
        return weeklyReportMapper.selectWeeklyCountLast7Days();
    }
}